package com.inossem.warehousemanage_tv.fragment;

import android.widget.ListView;
import android.widget.TextView;

import com.inossem.warehousemanage_tv.MarqueeTextView;
import com.inossem.warehousemanage_tv.R;
import com.inossem.warehousemanage_tv.adapter.PurchaseAdapter;
import com.inossem.warehousemanage_tv.bean.BaseBean;
import com.inossem.warehousemanage_tv.bean.MaterialBean;
import com.inossem.warehousemanage_tv.bean.RFIDBean;
import com.inossem.warehousemanage_tv.bean.SocketInfoBean;
import com.inossem.warehousemanage_tv.iot.SendSocketData;
import com.inossem.warehousemanage_tv.utils.Api;
import com.inossem.warehousemanage_tv.utils.requestUtils.InossemCallback;
import com.inossem.warehousemanage_tv.utils.requestUtils.RetrofitUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Response;

/**
 * Created by es on 2019/5/17.
 */

public class PurchaseFragment extends BaseFragment {

    private TextView purchase_id, purchase_scare,logistics_id,statusTv;
    private List<MaterialBean.MaterielListBean> materialBeanList = new ArrayList<MaterialBean.MaterielListBean>();
    private MarqueeTextView notice;
    private ListView listView;

    private String socketUrl = "0";
    private String socketPort = "0";
    private SendSocketData sendSocketData;

    @Override
    public int initLayout() {
        return R.layout.activity_purchase;
    }

    @Override
    public void initView() {
        notice = mView.findViewById(R.id.notice);
        purchase_id = mView.findViewById(R.id.purchase_id);
        purchase_scare = mView.findViewById(R.id.purchase_scare);
        logistics_id = mView.findViewById(R.id.logistics_id);
        listView = mView.findViewById(R.id.lv);
        statusTv = mView.findViewById(R.id.statusTv);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initClick() {
        getSocketInfo();
    }

    private void getSocketInfo() {
        RetrofitUtils.getRetrofit(mActivity, "王月玲/王斯宇", "王冬", "物料", "SocketInfo").create(Api.class).getSocketInfo().enqueue(new InossemCallback<BaseBean<List<SocketInfoBean>>>() {
            @Override
            public void success(Response<BaseBean<List<SocketInfoBean>>> response) {
                if (response.body().getBody() == null || response.body().getBody().isEmpty())
                    return;
                socketPort = response.body().getBody().get(0).getSocket_port();
                socketUrl = response.body().getBody().get(0).getSocket_url();

                startSocket();
            }

            @Override
            public void failure(Response<BaseBean<List<SocketInfoBean>>> response) {

            }
        });
    }

    private void startSocket() {
        sendSocketData = new SendSocketData(socketUrl, Integer.parseInt(socketPort));
        sendSocketData.setOnButtonClickListener(this::getData);
    }

    private void getData(String data) {
        RetrofitUtils.getRetrofit(mActivity, "王月玲/王斯宇", "王冬", "物料", "物料").create(Api.class).getMateriel_list(new RFIDBean(data)).enqueue(new InossemCallback<BaseBean<MaterialBean>>() {
            @Override
            public void success(Response<BaseBean<MaterialBean>> response) {
                MaterialBean bean = response.body().getBody();
                purchase_scare.setText(String.format(getResources().getString(R.string.number), String.valueOf(bean.getScanned_number()), String.valueOf(bean.getAll_scan_number())));
                logistics_id.setText(bean.getMessage());
                StringBuilder s = new StringBuilder();
                for (int i = 0; i < materialBeanList.size(); i++) {
                    MaterialBean.MaterielListBean bean1 = materialBeanList.get(i);
                    if (bean1.getFlag().equals("0")) {
                        s.append(bean1.getRfid()).append("、");
                    }
                }
                s.append("未读取！");
                notice.setText(s.toString());
                purchase_id.setText("采购订单: "+bean.getPurchase_order_name());
                materialBeanList = response.body().getBody().getMateriel_list();
                statusTv.setText(bean.getAll_scan_number()==bean.getScanned_number()?"已完成":"未完成");

                PurchaseAdapter adapter = new PurchaseAdapter(mActivity, materialBeanList);
                listView.setAdapter(adapter);
            }

            @Override
            public void failure(Response<BaseBean<MaterialBean>> response) {

            }

        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        sendSocketData = null;
    }

}
